package com.alibaba.dubbo.remoting.interceptor;

import java.net.InetAddress;
import java.util.Map;

import com.alibaba.dubbo.remoting.Interceptor;
import com.alibaba.dubbo.remoting.RemoteCalling;

/**
 * 链式管理
 * @author kingbo.ruan
 * 2014.09.16
 *
 */
public class InterceptorWrapperChain extends InterceptorWrapper{
	protected Interceptor next;
	
	
	public InterceptorWrapperChain(){
		super();
	}
	
	public InterceptorWrapperChain(Interceptor interceptor){
		super(interceptor);
	}
	
	public void setNext(Interceptor next){
		this.next = next;
	}

	@Override
	public boolean acceptAfterConnected(InetAddress remoteAddress) {
		if(!super.acceptAfterConnected(remoteAddress)) return false;
		if(this.next != null){
			return next.acceptAfterConnected(remoteAddress);
		}
		return true;
	}
	
	
	
	
	@Override
	public boolean acceptAfterHead(InetAddress remoteAddress, int total, int body) {
		if(!super.acceptAfterHead(remoteAddress, total, body)) return false;
		if(this.next != null){
			return next.acceptAfterHead(remoteAddress, total, body);
		}
		return true;
	}

	@Override
	public boolean acceptAfterDecodeHeader(InetAddress remoteAddress,  Map<String,String> headers, int data) {
		if(!super.acceptAfterDecodeHeader(remoteAddress, headers, data)) return false;
		if(this.next != null){
			return next.acceptAfterDecodeHeader(remoteAddress, headers, data);
		}
		return true;
	}
	
	
	@Override
	public boolean acceptAfterDecodeBody(InetAddress remoteAddress,  Map<String,String> headers, RemoteCalling calling) {
		if(!super.acceptAfterDecodeBody(remoteAddress, headers, calling)) return false;
		if(this.next != null){
			return next.acceptAfterDecodeBody(remoteAddress, headers, calling);
		}
		return true;
	}
	
	@Override
	public boolean acceptBeforeEncodeHeader(InetAddress remoteAddress,  Map<String,String> headers) {
		if(!super.acceptBeforeEncodeHeader(remoteAddress, headers)) return false;
		if(this.next != null){
			return next.acceptBeforeEncodeHeader(remoteAddress, headers);
		}
		return true;
	}
	
	
	@Override
	public boolean acceptBeforeEncodeBody(InetAddress remoteAddress,  Map<String,String> headers) {
		if(!super.acceptBeforeEncodeBody(remoteAddress, headers)) return false;
		if(this.next != null){
			return next.acceptBeforeEncodeBody(remoteAddress, headers);
		}
		return true;
	}
}
